package com.raos.mongo.demo.dao;

import com.raos.mongo.demo.entity.Member;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.core.common.mongo.BaseDaoSupport;
import javax.core.common.mongo.QueryRule;
import java.util.List;

/**
 * Member 持久层
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/8/30 22:02
 */
@Repository
public class MemberDao extends BaseDaoSupport<Member, Long> {

    @Override
    protected String getPKColumn() {
        return "id";
    }

    @Resource(name = "mongoTemplate")
    protected void setTemplate(MongoTemplate template) {
        super.setTemplate(template);
    }

    public List<Member> selectByName(String name) throws Exception {
        QueryRule queryRule = QueryRule.getInstance()
                        .andEqual("name", name)
                        .addAscOrder("name")
                        .addAscOrder("id");

        return super.find(queryRule);
//		return super.getAll();
    }

    public List<Member> selectAll() throws Exception {
        return super.getAll();
    }

    public boolean insterOne(Member m) throws Exception {
        Member member = super.insertAndReturn(m);
        return member.getId().toString() != null;
    }

    public boolean updataOne(Member m) throws Exception {
        long count = super.update(m);
        return count > 0;
    }

    public boolean deleteOne(Member m) throws Exception {
        long count = super.deleteEntity(m);
        return count > 0;
    }

}
